#!/usr/bin/python3
from openpyxl import load_workbook
from openpyxl.cell import Cell
import os, gc
from .Base_Plugin import Base_Plugin


class Sheet_Clear_Expression(Base_Plugin):
    """清理工程文件中的表达式的插件"""

    def __init__(self, context) -> None:
        super().__init__()
        self.context = context
        self.rename_delimiter = "->"
        self.modify_table_name_style_fill = None
        self.modify_column_name_style_fill = None
        self.normal_style_fill = None

    def clear_table_name(self, sheet):
        _cell: Cell = sheet["A2"]
        if self.rename_delimiter in _cell.value:
            _cell.value = _cell.value.split(self.rename_delimiter)[1]
            _cell.fill = self.modify_table_name_style_fill
        else:
            _cell.fill = self.normal_style_fill

    def clear_table_description(self, sheet):
        if self.rename_delimiter in sheet["A1"].value:
            sheet["A1"].value = sheet["A1"].value.split(self.rename_delimiter)[1]
            sheet["A1"].fill = self.modify_table_name_style_fill
        else:
            sheet["A1"].fill = self.normal_style_fill

    def clear_column_name(self, sheet):
        for row in sheet.rows:
            if row[1].row < 4:
                continue

            if row[1].value is None:
                break

            if self.rename_delimiter in row[1].value:
                row[1].value = row[1].value.split(self.rename_delimiter)[1]
                row[1].fill = self.modify_column_name_style_fill
            else:
                row[1].fill = self.normal_style_fill

    def run(self, workspace, source):
        workbook = load_workbook(os.path.join(workspace, source))
        _directory = workbook["目录"]
        _style_config = workbook["工程配置"]
        self.modify_table_name_style_fill = _style_config[
            "C5"
        ].fill.copy()  # 修改表名的样式
        self.modify_column_name_style_fill = _style_config[
            "C6"
        ].fill.copy()  # 修改字段名的样式
        self.normal_style_fill = _style_config["C2"].fill.copy()  # 未作任何变更的样式
        for row in _directory.rows:
            if row[1].row < 2:
                continue
            table_name = row[1].value
            if table_name is None:
                break
            try:
                table_name = table_name.strip()
                table_description = row[2].value.strip()

                self.clear_table_name(workbook[table_description])
                self.clear_table_description(workbook[table_description])
                self.clear_column_name(workbook[table_description])
                self.processed += 1
            except Exception as ex:
                self.print(f"""处理{table_name}出错了""")
                raise ex

        return_val = None
        if self.processed > 0:
            saved_name = r"ClearExpression.xlsx"
            new_path = os.path.join(workspace, saved_name)
            if os.path.exists(new_path):
                os.remove(new_path)
            workbook.save(new_path)
            return_val = saved_name
        else:
            return_val = source
        del workbook
        gc.collect()
        return return_val
